﻿using Jay.Sc2.Bof.Lib.State;

namespace Jay.Sc2.Bof.Lib.Action.Terran
{
	public class ActionProduceHellion : ActionProduceTerran
	{
		public static ActionProduceHellion Instance = new ActionProduceHellion();

		private ActionProduceHellion() : base(30, 100, 0, 1) { }

		public override int AvailableProducer(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return
				(state.Factory.AvailableForProduction) +
				(state.FactoryTechLab.AvailableForProduction) +
				(state.FactoryReactor.AvailableForProduction);
		}

		public override bool HaveTechnology(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			return
				(state.Factory.Total) +
				(state.FactoryTechLab.Total) +
				(state.FactoryReactor.Total) > 0;
		}

		public void OnProductionFinished(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			state.Hellion++;
		}

		public override void Execute(IBuildOrder bo)
		{
			TerranState state = bo.State as TerranState;
			int fromFactory = (state.Factory.AvailableForProduction);
			int fromFactoryTechLab = (state.FactoryTechLab.AvailableForProduction);
			int fromFactoryReactor = (state.FactoryReactor.AvailableForProduction);
			if (fromFactoryReactor > 0)
			{
				base.Execute(bo);
				state.FactoryReactor.Production++;
				;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnHellion", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.FactoryReactor.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else if (fromFactory > 0)
			{
				base.Execute(bo);
				state.Factory.Production++;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnHellion", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.Factory.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else if (fromFactoryTechLab > 0)
			{
				base.Execute(bo);
				state.FactoryTechLab.Production++;
				bo.AddAction(state.Time + BuildTime, new ActionGeneric("ActionSpawnHellion", (ActionGeneric.ActionExecuteDelegate)delegate(IBuildOrder ibo)
				{
					TerranState ibostate = ibo.State as TerranState;
					ibostate.FactoryTechLab.Production--;
					OnProductionFinished(ibo);
				}));
			}
			else throw new InvalidActionException();
		}


		private IAction[,] m_Requirements;
		public override IAction[,] Requirements
		{
			get
			{
				if (m_Requirements == null)
					m_Requirements = new IAction[,] { { ActionProduceFactory.Instance } };
				return m_Requirements;
			}
		}

	}
}
